package ru.budget.mobile.domain.usecase.consumption

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import ru.budget.mobile.core.Response
import ru.budget.mobile.core.ResponseStates
import ru.budget.mobile.domain.entities.ConsumptionDomain
import ru.budget.mobile.domain.repository.ConsumptionRepository
import ru.budget.mobile.ui.entities.ConsumptionUI

class GetConsumptionListUseCase(
    private val repository: ConsumptionRepository
) {
    operator fun invoke(): Flow<Response<List<ConsumptionUI>>> = flow {
        emit(Response.Loading(state = ResponseStates.LOADING))
        emit(
            repository.consumptionList().map {
                it.map(mapper)
            }
        )
    }

    private val mapper = { consumption: ConsumptionDomain ->
        ConsumptionUI(
            id = consumption.id,
            title = consumption.title,
            type = consumption.type,
            category = consumption.category,
            sum = consumption.sum,
            datetime = consumption.datetime
        )
    }
}